#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;
const char *filename = "./contacts.bin";
void AddPeople(contacts::PeopleInfo *people_ptr)
{
    cout << "请输入联系人姓名：";
    string name;
    getline(cin, name);
    people_ptr->set_name(name);

    cout << "请输入联系人年龄：";
    int age;
    cin >> age;
    people_ptr->set_age(age);
    cin.ignore(256, '\n');

    for (int i = 0;; i++)
    {
        cout << "请输入联系人电话（回车结束）：";
        string phone_number;
        getline(cin, phone_number);
        if (phone_number.empty())
            break;

        contacts::PeopleInfo_Phone *phone = people_ptr->add_phone();
        phone->set_phone_num(phone_number);

        cout << "选择此电话类型 (1、移动电话 2、固定电话) : ";
        int type;
        cin >> type;
        cin.ignore(256, '\n');
        switch (type)
        {
        case 1:
            phone->set_type(contacts::PeopleInfo_Phone_PhoneType_MP);
            break;
        case 2:
            phone->set_type(contacts::PeopleInfo_Phone_PhoneType_TEL);
            break;
        default:
            cout << "非法选择！" << endl;
            break;
        }
    }
    contacts::Address address;

    string home_address;
    cout << "请输入家庭住址：";
    getline(cin, home_address);
    address.set_home_address(home_address);

    string unit_address;
    cout << "请输入公司地址：";
    getline(cin, unit_address);
    address.set_unit_address(unit_address);

    google::protobuf::Any *data = people_ptr->mutable_data();
    data->PackFrom(address);

    cout << "选择添加⼀个其他联系⽅式 (1、qq号 2、微信号) : ";
    int other_contact;
    cin >> other_contact;
    cin.ignore(256, '\n');
    if (1 == other_contact)
    {
        cout << "请输⼊qq号: ";
        string qq;
        getline(cin, qq);
        people_ptr->set_qq(qq);
    }
    else if (2 == other_contact)
    {
        cout << "请输⼊微信号: ";
        string weixin;
        getline(cin, weixin);
        people_ptr->set_weixin(weixin);
    }
    else
    {
        cout << "⾮法选择，该项设置失败！" << endl;
    }
    for (int i = 1;; i++)
    {
        cout << "请输⼊备注" << i << "标题 (只输⼊回⻋完成备注新增): ";
        string remark_key;
        getline(cin, remark_key);
        if (remark_key.empty())
        {
            break;
        }

        cout << "请输⼊备注" << i << "内容: ";
        string remark_value;
        getline(cin, remark_value);
        people_ptr->mutable_remark()->insert({remark_key, remark_value});
    }
    cout << "联系人添加成功！" << endl;
}
int main()
{
    contacts::contacts contacts;
    fstream input(filename, ios::in | ios::binary);
    if (!input)
    {
        cerr << "文件打开失败！" << endl;
        input.close();
        return -1;
    }
    else if (!contacts.ParseFromIstream(&input))
    {
        cerr << "文件反序列化失败！" << endl;
        input.close();
        return -1;
    }
    else
    {
    }

    // 添加联系人
    AddPeople(contacts.add_constact());

    // 序列化并持续化到文件
    fstream output(filename, ios::out | ios::binary | ios::trunc);
    if (!output)
    {
        cerr << "文件打开失败！" << endl;
        output.close();
        return -1;
    }
    string contact_str;
    if (!contacts.SerializeToOstream(&output))
    {
        cerr << "文件序列化失败！" << endl;
        return -1;
    }
    input.close();
    output.close();
    return 0;
}